import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mac
 * Date: 2022-11-07
 * Time: 19:49
 */
class Test {
    public void func(){
        System.out.println("func()");
    }
}
public class TestDemo2 {
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1, "hello");
        map.put(2, "bit");
        map.put(3, "hello");
        map.put(4, "lambda");
        /*map.forEach(new BiConsumer<Integer, String>(){
            @Override
            public void accept(Integer k, String v){
                System.out.println(k + "=" + v);
            }
        });*/

        //lambda
        map.forEach((key, value)-> System.out.println(key + "=" + value));
    }
    public static void main2(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("zello");
        list.add("bit");
        list.add("hello");
        list.add("lambda");

        //lambda
        /*list.forEach(new Consumer<String>(){
            @Override
            public void accept(String str){
                //简单遍历集合中的元素。
                System.out.print(str+" ");
            }
        });*/

        list.forEach(s -> System.out.print(s + " "));

        //lambda
        //list.sort((o1, o2) -> o1.compareTo(o2));
        list.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });

        System.out.println();
        list.forEach(s -> System.out.print(s + " "));


    }

    public static void main1(String[] args) {
        int a = 100;
        //想要捕获变量，必须不可修改
        new Test(){
            @Override
            public void func() {
                System.out.println("我是内部类，且重写了func这个方法！");
                System.out.println("捕获变量：要么是常量，要么未修改过" + a);
            }
        }.func();
    }

}
